import 'package:twelve_animals/global.dart';
import 'package:twelve_animals/iconfont.dart';
import 'package:twelve_animals/main_manager.dart';
import 'package:twelve_animals/pages/index_page.dart';
import 'package:twelve_animals/pages/match_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:titled_navigation_bar/titled_navigation_bar.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Provider.debugCheckInvalidValueType = null;
  runApp(
    MultiProvider(
      child: MyApp(),
      providers: [
        ChangeNotifierProvider.value(value: MainManager()..init()),
      ],
    ),
  );
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _currentIndex = 0;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "十二生肖",
      theme: ThemeData(
        brightness: Brightness.light,
        primaryColor: Global.backgroundColor,
      ),
      home: Scaffold(
        body: SizedBox.expand(
          child: PageView(
            controller: _pageController,
            onPageChanged: (index) {
              setState(() => _currentIndex = index);
            },
            children: <Widget>[
              IndexPage(),
              MatchPage(),
            ],
          ),
        ),
        bottomNavigationBar: TitledBottomNavigationBar(
          currentIndex: _currentIndex,
          reverse: true,
          enableShadow: false,
          activeColor: Global.mainColor,
          onTap: (index) {
            setState(() => _currentIndex = index);
            _pageController.jumpToPage(index);
          },
          items: [
            TitledNavigationBarItem(
              title: Text(
                '十二生肖运势',
                style: TextStyle(
                  color: Global.mainColor,
                  fontSize: 12,
                  fontWeight: FontWeight.w600,
                ),
              ),
              icon: IconFont.wode_shengxiao,
            ),
            TitledNavigationBarItem(
              title: Text(
                '十二生肖匹配',
                style: TextStyle(
                  color: Global.mainColor,
                  fontSize: 12,
                  fontWeight: FontWeight.w600,
                ),
              ),
              icon: IconFont.hetaisui,
            ),
          ],
        ),
      ),
    );
  }
}
